﻿Imports System.Net
Imports System.IO

Public Class ConexionFTP

    Const PRE_FTP As String = "ftp://"

    Private _Server As String = ""
    Private _User As String = ""
    Private _Password As String = ""
    Private _Port As String = ""



    Public Sub New(ByVal Server As String, ByVal User As String, ByVal Password As String, ByVal Port As String)
        Me._Server = Server
        Me._User = User
        Me._Password = Password
        Me._Port = Port
        Conectar()
    End Sub

    Public Function Conectar() As String
        
        Return ""
    End Function

    ''' <summary>
    ''' This function list the directory passed by parameter
    ''' </summary>
    ''' <param name="directory"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ListFiles(ByVal directory As String) As List(Of String)
        Dim list As New List(Of String)
        list.Clear() 'if the list isn't empty

        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(PRE_FTP & Me._Server & directory), FtpWebRequest)

        FTP.Credentials = New NetworkCredential(Me._User, Me._Password)
        FTP.KeepAlive = True
        FTP.Method = WebRequestMethods.Ftp.ListDirectory
        Dim Response As FtpWebResponse = FTP.GetResponse()

        Dim responseStream As Stream = Response.GetResponseStream
        Dim reader = New StreamReader(responseStream)

        While Not reader.EndOfStream
            list.Add(reader.ReadLine())
        End While

        Return list
    End Function


    'Public Function
    Public Function DescargarArchivo(ByVal usuario As String, ByVal password As String) As Boolean
        'Dim localFile As String = "C:\youpdf"

        'Dim remoteFile As String = "uno.txt"

        '' colocamos el nombre de usuario y password respectivo para acceder al server, si este no poseyera, dejar solo las comillas, osea ""

        'Dim URI As String = host & remoteFile ' nombre completo de la ruta del archivo
        'Dim ftp As System.Net.FtpWebRequest = CType(Net.FtpWebRequest.Create(URI), Net.FtpWebRequest)
        'ftp.Credentials = New System.Net.NetworkCredential(usuario, password)
        'ftp.KeepAlive = False
        'ftp.UseBinary = True
        'ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

        'Try
        '    Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
        '        Using responseStream As IO.Stream = response.GetResponseStream
        '            Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
        '                Dim buffer(2047) As Byte
        '                Dim read As Integer = 0
        '                Do
        '                    read = responseStream.Read(buffer, 0, buffer.Length)
        '                    fs.Write(buffer, 0, read)
        '                Loop Until read = 0
        '                responseStream.Close()
        '                fs.Flush()
        '                fs.Close()
        '            End Using
        '            responseStream.Close()
        '        End Using
        '        response.Close()

        '    End Using
        'Catch ex As Exception

        '    MsgBox(ex.Message)

        'End Try

        Return ""
    End Function


End Class
